<?php

include('db.php');

//retourne la liste des putes
function show_bitches(){
    global $dbh;
     $sql="SELECT *
            FROM bitch";
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $bitches = $stmt->fetchAll();
    return $bitches;
    
}


//Obtenir les informations d'une pute à partir d'un ID
function select_bitch($bitch_id){
    global $dbh;

    $sql="SELECT *
            FROM bitch
            WHERE id=:id";
    $stmt = $dbh->prepare($sql);
    $stmt->bindValue(":id", $bitch_id, PDO::PARAM_INT);
    $stmt->execute();
    $bitch = $stmt->fetch();
    return $bitch;
}

//retourne les différents services
function show_services(){
    global $dbh;
     $sql="SELECT *
            FROM services_cost";
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $services = $stmt->fetchAll();
    return $services;
}

//retourne les feedback d'une pute
function bitch_feedback($bitch_id){
    global $dbh;
    $sql="SELECT *
          FROM user_feedback";
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $feedback = $stmt->fetchAll();
    return $feedback;
    
}

//Obtient la moyenne des notes utilisateurs
function bitch_note($bitch_id){
    global $dbh;
    $sql="SELECT avg(note)
          FROM user_feedback";
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $note = $stmt->fetch();
    return $note;
}

//Ajoute un feedback à une pute
function add_bitch_feedback(array $feed){
    global $dbh;
    $sql="INSERT INTO user_feedback VALUES ('', :bitchID, :note, :feedback, :username)";
    $stmt = $dbh->prepare($sql);
    $stmt->bindValue(":bitchID", $feed['bitch_id'], PDO::PARAM_INT);
    $stmt->bindValue(":note", $feed['note'], PDO::PARAM_INT);
    $stmt->bindValue(":feedback", $feed['feedback'], PDO::PARAM_STR);
    $stmt->bindValue(":username", $feed['username'], PDO::PARAM_STR);
    
    $stmt->execute();
}

//Recherche une pute selon les critères données
function find_bitch(array $critere){
    global $dbh;
    $sql="SELECT * FROM bitch
          WHERE sexe LIKE :sexe
          AND age BETWEEN :age1 AND :age2
          AND height BETWEEN :taille1 AND :taille2
          AND weight BETWEEN :poids1 AND :poids2
          AND hair_color LIKE :hair_color
          AND boobs >= :mensuration_taille
          AND boobs_bonnet LIKE :mensuration";
           
    $stmt = $dbh->prepare($sql);
    $stmt->bindValue(":sexe", "%".$critere['sexe']."%", PDO::PARAM_STR);
    $stmt->bindValue(":age1", $critere['age1'], PDO::PARAM_INT);
    $stmt->bindValue(":age2", $critere['age2'], PDO::PARAM_INT);
    $stmt->bindValue(":taille1", $critere['taille1'], PDO::PARAM_INT);
    $stmt->bindValue(":taille2", $critere['taille2'], PDO::PARAM_INT);
    $stmt->bindValue(":poids1", $critere['poids1'], PDO::PARAM_INT);
    $stmt->bindValue(":poids2", $critere['poids2'], PDO::PARAM_INT);
    $stmt->bindValue(":hair_color", "%".$critere['hair_color']."%", PDO::PARAM_STR);
    $stmt->bindValue(":mensuration", "%".$critere['bonnet']."%", PDO::PARAM_STR);
    $stmt->bindValue(":mensuration_taille", $critere['mensuration'], PDO::PARAM_INT);
   try {
    $stmt->execute();
    $bitches = $stmt->fetchAll();
    return $bitches;
   }
 catch (Exception $e){
     echo $e;
 }
}


function convertCoords ($array, $max_km, $user_latitude, $user_longitude){
    $r = 6366000;
 
    $lat2 = $array['latitude'];
    $lon2 = $array['longitude'];
 
    /**
    * Conversion des entrées en ° vers des Radians
    */
    $user_latitude = deg2rad($user_latitude);
    $user_longitude = deg2rad($user_longitude);
    $lat2 = deg2rad($lat2);
    $lon2 = deg2rad($lon2);
        
    /**
    * Formule simple
    * d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
    */
    $ds= acos(sin($user_latitude)*sin($lat2)+cos($user_latitude)*cos($lat2)*cos($user_longitude-$lon2));
    //$ds= acos(sin(deg2rad($user_latitude))*sin(deg2rad($lat2))+cos(deg2rad($user_latitude))*cos(deg2rad($lat2))*cos(deg2rad($user_longitude-$lon2)));
    $dsr = $ds * $r;
 
    /**
    * Formule précise
    * d=2*asin(
                sqrt(
                    (sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2)
                )
    */
    $dp = 2 * asin(
                sqrt(
                    pow( sin(($user_latitude-$lat2)/2) , 2) + cos($user_latitude)*cos($lat2)* pow( sin(($user_longitude-$lon2)/2) , 2)
                )
            );
    $dpr = $dp * $r;
     
    echo "
    Sorties :
    ds:$ds
    dsr:$dsr(km)
    ";

    $longitude = $array['longitude'];
    $latitude = $array['latitude'];
    foreach ($latitude as $donnees){
      $distance = 6371000*acos(cos(latitudeUser)*cos(latitudeMeuf)*cos(longitudeUser-longitudeMeuf)+sin(latitudeUser)*sin(latitudeMeuf));  
    }
    
}

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
		$theta = $lon1 - $lon2;

		$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
		$dist = acos($dist);
		$dist = rad2deg($dist);
		$miles = $dist * 60 * 1.1515;
		$unit = strtoupper($unit);

		if ($unit == "K") {
			return ($miles * 1.609344);
		} else if ($unit == "N") {
			return ($miles * 0.8684);
		} else {
			return $miles;
		}
	}
?>

